<template>
  <div class="app-container">
    <el-row align="center" margin-bottom="20px"><span id="HeadName">{{ this.deptName }}</span></el-row>
    <el-form v-show="showSearch" ref="queryForm" :inline="true" label-width="68px" size="small">

      <el-row>
        <el-form-item label="病历id">
          <el-input
            v-model="queryParams.MedicalId"
            clearable
            placeholder="请输入病历id"
          />
        </el-form-item>
        <el-form-item>
          <el-button icon="el-icon-search" size="mini" type="primary" @click="QueryByCaseNumber">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-row>
      <el-row>
        <el-form-item label="检查状态">
          <el-select v-model="queryParams.CheckState"
                     clearable placeholder="请选择检查状态">
            <el-option
              v-for="dict in dict.type.check_state"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button icon="el-icon-search" size="mini" type="primary" @click="QueryByCheckState">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-row>
    </el-form>


    <el-table v-loading="loading" :data="data">
      <el-table-column align="center" label="id" prop="CheckDetailed.Id"/>
      <el-table-column align="center" label="病历id" prop="CheckDetailed.MedicalId"/>
      <el-table-column align="center" label="检查项目" prop="CheckProjId"/>
      <el-table-column align="center" label="开立医生" prop="FromUserId"/>
      <el-table-column align="center" label="开立时间" prop="CheckDetailed.CreationTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.CheckDetailed.CreationTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="检查状态"
                       prop="CheckDetailed.CheckState">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.check_state" :value="scope.row.CheckDetailed.CheckState"/>
        </template>
      </el-table-column>
      <el-table-column align="center" label="单价" prop="CheckDetailed.Price"/>
      <el-table-column align="center" label="检查结果" prop="CheckDetailed.Result"/>
      <el-table-column align="center" label="结果时间" prop="CheckDetailed.ResultTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.CheckDetailed.ResultTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" class-name="small-padding fixed-width" label="操作">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['system:CheckDetailed:edit']"
            icon="el-icon-edit"
            size="mini"
            type="text"
            @click="setCheckState(scope.row)"
          >登记
          </el-button>
          <el-button
            v-hasPermi="['system:CheckDetailed:remove']"
            icon="el-icon-edit-outline"
            size="mini"
            type="text"
            @click="handleUpdate(scope.row)"
          >结果录入
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改检查详情对话框 -->
    <el-dialog :title="title" :visible.sync="open" append-to-body width="500px">
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="病历id" prop="MedicalId">
          <el-input v-model="form.MedicalId" placeholder="请输入病历id" :disabled="true"/>
        </el-form-item>
        <el-form-item label="开立时间" prop="CreationTime">
          <el-date-picker v-model="form.CreationTime"
                          clearable
                          placeholder="请选择开立时间"
                          type="date"
                          value-format="yyyy-MM-dd" :disabled="true">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="检查目的和要求" prop="Position">
          <el-input v-model="form.Position" placeholder="请输入内容" type="textarea" :disabled="true"/>
        </el-form-item>
        <el-form-item label="检查状态" prop="CheckState">
          <el-select v-model="form.CheckState"
                     placeholder="请选择检查状态" :disabled="true">
            <el-option
              v-for="dict in dict.type.check_state"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="单价" prop="Price">
          <el-input v-model="form.Price" placeholder="请输入单价" :disabled="true"/>
        </el-form-item>
        <el-form-item label="检查时间" prop="InspectTime">
          <el-date-picker v-model="form.InspectTime"
                          clearable
                          placeholder="请选择检查时间"
                          type="date"
                          value-format="yyyy-MM-dd" :disabled="true">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="检查结果" prop="Result">
          <el-input v-model="form.Result" placeholder="请输入内容" type="textarea"/>
        </el-form-item>
        <el-form-item label="结果时间" prop="ResultTime">
          <el-date-picker v-model="form.ResultTime"
                          clearable
                          placeholder="请选择结果时间"
                          type="date"
                          value-format="yyyy-MM-dd">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  enter,
  getCheckDetailed1,
  getCheckDetailed2,
  listCheckDetailed,
  setCheckState
} from "@/api/system/CheckDetailed";
import {getDept} from "@/api/system/dept";

export default {
  name: "CheckDetailed",

  dicts: ['check_state'],

  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      // 检查详情表格数据
      data: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      userId: 1,
      deptId: 0,
      deptName: "",
      // 查询参数
      queryParams: {
        current: 1,
        size: 10,
        MedicalId: null,
        CheckState: null,
      },
      // 表单参数
      form: {},
      // 表单校验
    };
  },
  mounted() {
    this.userId = localStorage.getItem("userId")
    console.log(this.userId)
    if (this.userId != 1) {
      this.deptId = localStorage.getItem("deptId")
    }
    this.deptName = localStorage.getItem("deptName");

    this.getList();
  },

  methods: {
    /** 查询检查详情列表 */
    getList() {
      this.loading = true;
      listCheckDetailed(this.deptId).then(response => {
        this.data = response;
        this.loading = false;
      });
    },

    setCheckState(row) {
      console.info(row.CheckDetailed.Id)
      setCheckState(row.CheckDetailed.Id, this.userId).then(() => {
        this.getList()
      })
    },
    enter(row) {
      enter(row.CheckDetailed).then(() => {
        this.getList()
      })
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.form = row.CheckDetailed
      this.open = true;
      this.title = "结果录入";
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        Id: null,
        MedicalId: null,
        CheckProjId: null,
        DeptId: null,
        FromUserId: null,
        CreationTime: null,
        Position: null,
        CheckState: null,
        Price: null,
        InspectTime: null,
        Result: null,
        ResultTime: null,
        EnterUserId: null,
        CheckUserId: null,
        DelMark: null
      };
      this.resetForm("form");
    },

    QueryByCaseNumber() {
      console.log(this.queryParams.MedicalId)
      getCheckDetailed1(this.queryParams.MedicalId).then(response => {
        this.data = response
      });
    },

    QueryByCheckState() {
      getCheckDetailed2(this.queryParams.CheckState).then(response => {
        this.data = response
      });
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },


    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          console.info(this.form)
          if (this.form.Id != null) {
            this.form.EnterUserId = this.userId
            enter(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
  }
};
</script>

<style>
div {
  font-family: "Helvetica Neue", Helvetica, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "微软雅黑", Arial, sans-serif;
}

#HeadName {
  font-size: 30px;
  color: #409eff;
  margin-bottom: 40px;
}

</style>
